package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RestController   // 使用RestController注解，返回值直接是json数据
public class UploadController {

   /* // 上传文件到本地磁盘
    @PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) {

        log.info("文件参数:{}, {}, {}", name, age, file);

        //获取原文件名
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成新的文件名
        String newFilename = UUID.randomUUID().toString() + suffix;
        //保存文件
        try {
            file.transferTo(new java.io.File( "D:\\upload\\" + newFilename));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }

        // 处理上传文件逻辑
        return Result.success();
    }*/
    @Autowired
    private AliOSSUtils aliOSSUtils;

    // 上传文件到OSS
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception{

        log.info("文件参数: {}", file.getOriginalFilename());
        String filePath = aliOSSUtils.upload(file);
        // 处理上传文件逻辑
        return Result.success(filePath);
    }

}
